package co.edu.usbcali.egrapico.vista.ambient;

import co.edu.usbcali.egrapico.modelo.Program;
import co.edu.usbcali.egrapico.vista.tools.JPanelTranferHandlerProgram;

public class PanelProgram extends javax.swing.JInternalFrame implements java.io.Serializable {

    LPaneProgram layeredPane;
    javax.swing.JScrollPane scroll1;
    String name;
    private int context = 0;
    private int ask = 0;
    private int tell = 0;
    private int msgS = 0;
    private int Objs = 0;

    /**
     * Creates a new instance of PanelDragAndDrop
     */
    public PanelProgram() {
        super("",//+(++openFrameCount) 
                true, //resizable
                true, //closable
                true, //maximizable
                true);//iconifiable                
        scroll1 = new javax.swing.JScrollPane();
        scroll1.setAutoscrolls(true);
        layeredPane = new LPaneProgram();
        scroll1.setViewportView(layeredPane);
        getContentPane().add(scroll1);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setTransferHandler(new JPanelTranferHandlerProgram());
        setSize(300, 300);
        //Set the window's location.
        setLocation(30, 30);
        setVisible(true);
        name = "";
    }

    public void loadFrame(Program aux) {

        java.util.Iterator<Program.Context> co = aux.getContext().iterator();
        while (co.hasNext()) {
            Program.Context coo = co.next();
            javax.swing.ImageIcon I1;
            LabelContext label = new LabelContext();
            if (coo.isIsclon()) {
                I1 = new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/cloncontex.gif"));
                label.setClon(true);
            } else {
                I1 = new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/context.gif"));
                label.setClon(false);
            }
            label.setIcon(I1);
            label.setHorizontalTextPosition(javax.swing.JLabel.CENTER);
            label.setVerticalTextPosition(javax.swing.JLabel.BOTTOM);
            label.setName(coo.getPhysicPart().getLabel());
            label.setTextToolTip(label.getName() + ": " + coo.getPhysicPart().getComment());
            label.setLocation(coo.getPhysicPart().getPosX(), coo.getPhysicPart().getPosY());
            label.setSize(90, 90);
            label.setTextPane(coo.getPhysicPart().getComment());
            this.addLayeredPane(label);
            context++;
        }
        java.util.Iterator<Program.ObjectLabel> oo = aux.getObjectLabel().iterator();
        while (oo.hasNext()) {
            Program.ObjectLabel ooo = oo.next();
            javax.swing.ImageIcon I5;
            LabelObject label5 = new LabelObject();
            if (ooo.isIsclon()) {
                I5 = new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clon_object.gif"));
                label5.setClon(true);
            } else {
                I5 = new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/object.gif"));
                label5.setClon(false);
            }
            label5.setIcon(I5);
            label5.setHorizontalTextPosition(javax.swing.JLabel.CENTER);
            label5.setVerticalTextPosition(javax.swing.JLabel.BOTTOM);
            label5.setName(ooo.getPhysicPart().getLabel());
            label5.setTextToolTip(label5.getName() + ": " + ooo.getPhysicPart().getComment());
            label5.setLocation(ooo.getPhysicPart().getPosX(), ooo.getPhysicPart().getPosY());
            label5.setSize(90, 90);
            label5.setTextPane(ooo.getPhysicPart().getComment());
            this.addLayeredPane(label5);

        }
        java.util.Iterator<Program.Ask> as = aux.getAsk().iterator();
        while (as.hasNext()) {
            @SuppressWarnings("LocalVariableHidesMemberVariable")
            Program.Ask ask = as.next();
            javax.swing.ImageIcon I2;
            LabelAsk label2 = new LabelAsk();
            if (ask.isIsclon() && ask.isHaveConsecuent()) {
                I2 = new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clonask.gif"));
                label2.setClon(true);
                label2.setIsConsecuent(true);
            } else if (!ask.isIsclon() && ask.isHaveConsecuent()) {
                I2 = new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/ask.gif"));
                label2.setClon(false);
                label2.setIsConsecuent(true);
            } else if (ask.isIsclon() && !ask.isHaveConsecuent()) {
                I2 = new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clon_ask1.gif"));
                label2.setClon(true);
                label2.setIsConsecuent(false);
                label2.changeItemMenu4();
                label2.hideConsecuent();
            } else {
                I2 = new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/askPreceding.gif"));
                label2.setClon(false);
                label2.setIsConsecuent(false);
                label2.changeItemMenu4();
                label2.hideConsecuent();
            }
            label2.setIcon(I2);
            label2.setHorizontalTextPosition(javax.swing.JLabel.CENTER);
            label2.setVerticalTextPosition(javax.swing.JLabel.BOTTOM);
            label2.setName(ask.getPhysicPart().getLabel());
            label2.setTextToolTip(label2.getName() + ": " + ask.getPhysicPart().getComment());
            label2.setLocation(ask.getPhysicPart().getPosX(), ask.getPhysicPart().getPosY());
            label2.setSize(90, 90);
            label2.setTextPane(ask.getPhysicPart().getComment());
            this.addLayeredPane(label2);
            this.ask++;

        }
        java.util.Iterator<Program.Tell> te = aux.getTell().iterator();
        while (te.hasNext()) {
            Program.Tell tel = te.next();
            javax.swing.ImageIcon I3;
            LabelTell label3 = new LabelTell();
            if (tel.isIsclon() && tel.isHaveConsecuent()) {
                I3 = new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clontellp.gif"));
                label3.setClon(true);
                label3.setIsConsecuent(true);
            } else if (!tel.isIsclon() && tel.isHaveConsecuent()) {
                I3 = new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/tell.gif"));
                label3.setClon(false);
                label3.setIsConsecuent(true);
            } else if (tel.isIsclon() && !tel.isHaveConsecuent()) {
                I3 = new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clontell.gif"));
                label3.setClon(true);
                label3.setIsConsecuent(false);
                label3.changeItemMenu4();
                label3.hideConsecuent();
            } else {
                I3 = new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/tellPreceding.gif"));
                label3.setClon(false);
                label3.setIsConsecuent(false);
                label3.changeItemMenu4();
                label3.hideConsecuent();
            }
            label3.setIcon(I3);
            label3.setHorizontalTextPosition(javax.swing.JLabel.CENTER);
            label3.setVerticalTextPosition(javax.swing.JLabel.BOTTOM);
            label3.setName(tel.getPhysicPart().getLabel());
            label3.setTextToolTip(label3.getName() + ": " + tel.getPhysicPart().getComment());
            label3.setLocation(tel.getPhysicPart().getPosX(), tel.getPhysicPart().getPosY());
            label3.setSize(90, 90);
            label3.setTextPane(tel.getPhysicPart().getComment());
            this.addLayeredPane(label3);
            tell++;
        }
        java.util.Iterator<Program.MsgSend> me = aux.getMsgSend().iterator();
        while (me.hasNext()) {
            Program.MsgSend msg = me.next();
            javax.swing.ImageIcon I4;
            LabelMsgSend label4 = new LabelMsgSend();
            if (msg.isIsclon() && msg.isHaveConsecuent()) {
                I4 = new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clonmsgsend.gif"));
                label4.setClon(true);
                label4.setIsConsecuent(true);
            } else if (!msg.isIsclon() && msg.isHaveConsecuent()) {
                I4 = new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/msgSend.gif"));
                label4.setClon(false);
                label4.setIsConsecuent(true);
            } else if (msg.isIsclon() && !msg.isHaveConsecuent()) {
                I4 = new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/clonmsgSendPreceding.gif"));
                label4.setClon(true);
                label4.setIsConsecuent(false);
                label4.changeItemMenu4();
                label4.hideConsecuent();
            } else {
                I4 = new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/msgSendPreceding.gif"));
                label4.setClon(false);
                label4.setIsConsecuent(false);
                label4.changeItemMenu4();
                label4.hideConsecuent();
            }
            label4.setIcon(I4);
            label4.setHorizontalTextPosition(javax.swing.JLabel.CENTER);
            label4.setVerticalTextPosition(javax.swing.JLabel.BOTTOM);
            label4.setName(msg.getPhysicPart().getLabel());
            label4.setTextToolTip(label4.getName() + ": " + msg.getPhysicPart().getComment());
            label4.setLocation(msg.getPhysicPart().getPosX(), msg.getPhysicPart().getPosY());
            label4.setSize(90, 90);
            label4.setTextPane(msg.getPhysicPart().getComment());
            this.addLayeredPane(label4);
            msgS++;
        }
    }

    public void setNameP(String n) {
        name = n;
    }

    public String getNameP() {
        return name;
    }

    public LPaneProgram getLPane() {
        return layeredPane;
    }

    public void setLPane(LPaneProgram e) {
        layeredPane = e;
    }

    public void addLayeredPane(LabelContext label) {
        label.setNameParent(getTitle());
        if (label.getName().equals("Context")) {
            label.setName(label.getName() + (++context));
            label.setText(label.getName());
            label.setTextToolTip(label.getName() + ": ");
        } else {
            context++;
        }
        layeredPane.addLabel(label);
        layeredPane.addLabeltoVector(label);
        try {
            setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
    }

    public void addLayeredPane(LabelAsk label) {
        label.setNameParent(getTitle());
        if (label.getName().equals("Ask")) {
            label.setName(label.getName() + (++ask));
            label.setText(label.getName());
            label.setTextToolTip(label.getName() + ": ");
        } else {
            ask++;
        }
        layeredPane.addLabel(label);
        layeredPane.addLabeltoVector(label);
        try {
            setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
    }

    public void addLayeredPane(LabelTell label) {
        label.setNameParent(getTitle());
        if (label.getName().equals("Tell")) {
            label.setName(label.getName() + (++tell));
            label.setText(label.getName());
            label.setTextToolTip(label.getName() + ": ");
        } else {
            tell++;
        }
        layeredPane.addLabel(label);
        layeredPane.addLabeltoVector(label);
        try {
            setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
    }

    public void addLayeredPane(LabelMsgSend label) {
        label.setNameParent(getTitle());
        if (label.getName().equals("MsgSend")) {
            label.setName(label.getName() + (++msgS));
            label.setText(label.getName());
            label.setTextToolTip(label.getName() + ": ");
        } else {
            msgS++;
        }
        layeredPane.addLabel(label);
        layeredPane.addLabeltoVector(label);
        try {
            setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
    }

    public void addLayeredPane(LabelObject label) {
        label.setNameParent(getTitle());
        layeredPane.addLabel(label);
        layeredPane.addLabeltoVector(label);
        try {
            setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
    }

    public void setUndoLPane() {
        layeredPane.Undo();
    }

    public void setRedoLPane() {
        layeredPane.Redo();
    }

    public int getRedosize() {
        return layeredPane.getRedoSize();
    }

    public int getUndosize() {
        return layeredPane.getUndoSize();
    }
}